package com.android.application;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;

public class MP3Activity extends Activity {
    /** Called when the activity is first created. */
	
	private final String TAG = "MP3Activity";
	
	private EditText txtFileSearch;
	private CheckBox chkReplay;
	private MediaPlayer mpPlayer = new MediaPlayer();
	private ImageButton btnPlayPause;
	private ImageButton btnStop;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.audio);
        
        // Initialize EditText
        txtFileSearch = (EditText) findViewById(R.id.txtFilePath);
        txtFileSearch.setText("http://downloads.bbc.co.uk/podcasts/worldservice/tae/tae_20091116-1200b.mp3");
        
        // Initialize FileOpen Button 
        Button btn = (Button) findViewById(R.id.btnFileOpen);
        
        btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Read FileSearch EditText
				//		Load Media Player
			}
		});
        
        // Initialize Replay CheckBox
        chkReplay = (CheckBox) findViewById(R.id.chkReplay);
        chkReplay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Set Looping to MediaPlayer
				//		Check Status
				Log.d(TAG, "Replay CheckBox Clicked!");
				
				if (chkReplay.isChecked()) {
					mpPlayer.setLooping(true);
				} else {
					mpPlayer.setLooping(false);
				}
			}
		});
        
        // Initialize PlayPause Button
        btnPlayPause = (ImageButton) findViewById(R.id.btnPlayPause);        
        btnPlayPause.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Check MediaPlayer now Playing
				//		Update StartPause Button Image
				Log.d(TAG, "PlayPause Button Clicked!");
				
				if (mpPlayer.isPlaying()) {
					mpPlayer.pause();
					btnPlayPause.setImageResource(R.drawable.play);
					
				} else {
					// MediaPlayer Repare!!!!
					String str = txtFileSearch.getText().toString().trim();
					
					if (!str.equals("")) {
						try {
							mpPlayer.setDataSource(str);
							
							mpPlayer.prepare();
							
							mpPlayer.start();
							
							btnPlayPause.setImageResource(R.drawable.pause);
							
						} catch (IllegalArgumentException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IllegalStateException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
			
					}
				}
			}
		});
        
        // Initialize Stop Button
        btnStop = (ImageButton) findViewById(R.id.btnStop);
        btnStop.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Stop MediaPlayer
				//		Clear Resource
				Log.d(TAG, "Stop Button Clicked!");
				
				if (mpPlayer.isPlaying()) {
					mpPlayer.stop();
				}
			}
		});
        
    }
}